// mircoApp/horizon/component/deviceListPopup/deviceListPopup.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    popupKey: {
      type: String,
      value: ""
    },
    childItem: {
      type: null,
    },
    member_time_count: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    rechargeNum: "",
    search: "",
    radio: ""
  },
  storeList: [],
  lifetimes: {

  },
  observers: {
    "popupKey": function (boxshow) {
      console.log(boxshow, 'observers=>popupKey')
      this.setData({
        storeList: []
      })
      if (this.data.childItem?.store_id) {
        this.setData({
          radio: this.data.childItem.store_id
        })
      }

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    TapMore() {
      this.setData({
        page: this.data.page + 1
      })
      this.getData_edit('more');
    },
    getData_edit(key) {
      console.log(this.data.childItem)
      let urlStr = app.getNetAddresss("plugin.horizon.frontend.owner.searchStore");
      app._getNetWork({
        url: urlStr,
        data: {
          keyword: this.data.search,
          page: this.data.page
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            // console.log(res, 'dddddddddddddd')
            if (key == 'more') {
              this.setData({
                storeList: [...this.data.storeList, ...res.data.data],
                current_page: res.data.current_page,
                last_page: res.data.last_page
              })
            } else {
              this.setData({
                storeList: res.data.data,
                current_page: res.data.current_page,
                last_page: res.data.last_page
              })
            }

          } else {
            this.setData({
              storeList: [],
              current_page: 0,
              last_page: 0
            })
            // app.tips(res.msg)
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
    onClickLeft() {
      this.triggerEvent("close", false)
    },
    onClickRight() {
      // this.triggerEvent("close", true)
      if (this.data.popupKey == 'recharge') {
        let urlStr = app.getNetAddresss("plugin.horizon.frontend.machine.rechangeMachineTimes");
        app._getNetWork({
          url: urlStr,
          data: {
            machine_id: this.data.childItem.id,
            count: this.data.rechargeNum
          },
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              // console.log(res, 'dddddddddddddd')
              app.tips(res.msg)
              this.triggerEvent("close", true)
            } else {
              app.tips(res.msg)
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      } else {
        // 绑定关联门店
        console.log(this.data.radio,this.data.childItem.store_id)
        if(this.data.radio == this.data.childItem.store_id){
          this.triggerEvent("close", true)
          return
        }
        let urlStr = app.getNetAddresss("plugin.horizon.frontend.machine.changeMachineStore");
        app._getNetWork({
          url: urlStr,
          data: {
            store_id: this.data.radio,
            machine_id: this.data.childItem.id
          },
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              app.tips(res.msg)
              this.triggerEvent("close", true)
            } else {
              app.tips(res.msg)
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
    onChange(event) {
      console.log(event)
      this.setData({
        radio: event.detail,
      });
    },
    onClick(event) {
      const {
        name
      } = event.currentTarget.dataset;
      this.setData({
        radio: name,
      });
    },
    showModel(e) {
      let {
        key
      } = e.currentTarget.dataset;
      console.log(key)
      if (key == 'recharge') {
        this.setData({
          rechargeShow: true
        })
      }
    },
    searchPost() {
      this.setData({
        current_page: 0,
        last_page: 0,
        page: 1
      })
      this.getData_edit();
    },
  }
})